package com.miyu.common.utils.bookPage;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.common.utils.commonUtils.CommonCache;
import com.miyu.common.utils.ehcache.EhcacheUtil;
import com.miyu.common.utils.magic.GetBeanUtil;
import com.miyu.common.utils.redis.redisUtils.util.RedisListUtil;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 保存缓存数据
 *
 * @author miyu
 * @date 2022/07/12
 */
public class SaveCacheData implements Pipeline {

    /**
     * 过程
     *
     * @param resultItems 结果项目
     * @param task        任务
     */
    @Override
    public void process(ResultItems resultItems, Task task) {
        String key = resultItems.get("key");
        String type = resultItems.get("type");
        Object data = resultItems.get("data");
        if("1".equals(type)){
            if(StringUtils.isNotBlank(data.toString())){
                JSONArray d =  JSONArray.parseArray(data.toString());
                CommonCache.setJSONArrayByKey("bookEhcache",key,d);
            }
        }else {
            /*传入JSONObject数据*/
            if(StringUtils.isNotBlank(data.toString())){
                System.out.println(data.toString());
                JSONObject d = JSONObject.parseObject(data.toString());
                CommonCache.setJSONObjectByKey("bookEhcache",key,d);
            }
        }
    }
}
